home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / lightning-0.8-tb-win.xpi / js / calWcapErrors.js < prev    next >
Text File  |  2007-12-10  |  25KB  |  427 lines

  1. /* -*- Mode: javascript; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Sun Microsystems code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Sun Microsystems, Inc.
  19.  * Portions created by the Initial Developer are Copyright (C) 2007
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Daniel Boelzle <daniel.boelzle@sun.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. const NS_ERROR_INVALID_ARG = Components.results.NS_ERROR_INVALID_ARG;
  40.  
  41. //
  42. // Common netwerk errors:
  43. //
  44. const NS_ERROR_MODULE_BASE_OFFSET = 0x45;
  45. const NS_ERROR_MODULE_NETWORK = 6;
  46.  
  47. function generateFailure(module, code) {
  48.     // 1<<31 generates negative number, so use literal, don't use logical operators:
  49.     return (0x80000000 + ((module + NS_ERROR_MODULE_BASE_OFFSET) << 16) + code);
  50. }
  51.  
  52. function generateNetFailure(code) {
  53.     return generateFailure(NS_ERROR_MODULE_NETWORK, code);
  54. }
  55.  
  56. function getResultCode(err) {
  57.     if (err === undefined || err === null)
  58.         return NS_OK;
  59.     if (isNaN(err)) {
  60.         if (err instanceof nsIException)
  61.             return err.result;
  62.         else
  63.             return Components.results.NS_ERROR_FAILURE;
  64.     }
  65.     return err;
  66. }
  67.  
  68. function getErrorModule(err) {
  69.     var rc = getResultCode(err);
  70.     return (((rc >>> 16) & 0x7fff) - NS_ERROR_MODULE_BASE_OFFSET);
  71. }
  72.  
  73. function checkErrorCode(err, rcBits, maskBits) {
  74.     if (!maskBits) {
  75.         maskBits = 0;
  76.     }
  77.     var rc = getResultCode(err);
  78.     return ((rc ^ rcBits) >>> maskBits) == 0;
  79. }
  80.  
  81. // Cannot perform operation, because user is offline.
  82. // The following error codes have been adopted from
  83. // netwerk/base/public/nsNetError.h
  84. // and ought to be defined in IDL. xxx todo
  85.  
  86. // The requested action could not be completed while the networking
  87. // is in the offline state.
  88. const NS_ERROR_OFFLINE = generateNetFailure(16);
  89.  
  90. // The async request completed successfully.
  91. const NS_BINDING_SUCCEEDED = NS_OK;
  92.  
  93. const NS_BINDING_FAILED = generateNetFailure(1);
  94. const NS_BINDING_ABORTED = generateNetFailure(2);
  95. const NS_BINDING_REDIRECTED = generateNetFailure(3);
  96. const NS_BINDING_RETARGETED = generateNetFailure(4);
  97.  
  98. const g_nsNetErrorCodes = [
  99.     NS_BINDING_FAILED,
  100.     "The async request failed for some unknown reason.",
  101.     NS_BINDING_ABORTED,
  102.     "The async request failed because it was aborted by some user action.",
  103.     NS_BINDING_REDIRECTED,
  104.     "The async request has been redirected to a different async request.",
  105.     NS_BINDING_RETARGETED,
  106.     "The async request has been retargeted to a different handler.",
  107.     /*NS_ERROR_MALFORMED_URI*/ generateNetFailure(10),
  108.     "The URI is malformed.",
  109.     /*NS_ERROR_UNKNOWN_PROTOCOL*/ generateNetFailure(18),
  110.     "The URI scheme corresponds to an unknown protocol handler.",
  111.     /*NS_ERROR_CONNECTION_REFUSED*/ generateNetFailure(13),
  112.     "The connection attempt failed, for example, because no server was listening at specified host:port.",
  113.     /*NS_ERROR_PROXY_CONNECTION_REFUSED*/ generateNetFailure(72),
  114.     "The connection attempt to a proxy failed.",
  115.     /*NS_ERROR_NET_TIMEOUT*/ generateNetFailure(14),
  116.     "The connection was lost due to a timeout error.",
  117.     NS_ERROR_OFFLINE,
  118.     "The requested action could not be completed while the networking library is in the offline state.",
  119.     /*NS_ERROR_PORT_ACCESS_NOT_ALLOWED*/ generateNetFailure(19),
  120.     "The requested action was prohibited because it would have caused the networking library to establish a connection to an unsafe or otherwise banned port.",
  121.     /*NS_ERROR_NET_RESET*/ generateNetFailure(20),
  122.     "The connection was established, but no data was ever received.",
  123.     /*NS_ERROR_NET_INTERRUPT*/ generateNetFailure(71),
  124.     "The connection was established, but the data transfer was interrupted.",
  125.     /*NS_ERROR_NOT_RESUMABLE*/ generateNetFailure(25),
  126.     "This request is not resumable, but it was tried to resume it, or to request resume-specific data.",
  127.     /*NS_ERROR_ENTITY_CHANGED*/ generateNetFailure(32),
  128.     "It was attempted to resume the request, but the entity has changed in the meantime.",
  129.     /*NS_ERROR_REDIRECT_LOOP*/ generateNetFailure(31),
  130.     "The request failed as a result of a detected redirection loop.",
  131.     /*NS_ERROR_UNKNOWN_HOST*/ generateNetFailure(30),
  132.     "The lookup of a hostname failed. This generally refers to the hostname from the URL being loaded.",
  133.     /*NS_ERROR_UNKNOWN_PROXY_HOST*/ generateNetFailure(42),
  134.     "The lookup of a proxy hostname failed.",
  135.     /*NS_ERROR_UNKNOWN_SOCKET_TYPE*/ generateNetFailure(51),
  136.     "The specified socket type does not exist.",
  137.     /*NS_ERROR_SOCKET_CREATE_FAILED*/ generateNetFailure(52),
  138.     "The specified socket type could not be created."
  139.     ];
  140.  
  141. function netErrorToString(rc)
  142. {
  143.     if (!isNaN(rc) && getErrorModule(rc) == NS_ERROR_MODULE_NETWORK) {
  144.         var i = 0;
  145.         while (i < g_nsNetErrorCodes.length) {
  146.             // however rc is kept unsigned, our generated code signed,
  147.             // so == won't work here:
  148.             if ((g_nsNetErrorCodes[i] ^ rc) == 0)
  149.                 return g_nsNetErrorCodes[i + 1];
  150.             i += 2;
  151.         }
  152.     }
  153.     throw new Components.Exception("No known network error code: " + rc.toString(0x10),
  154.                                    NS_ERROR_INVALID_ARG);
  155. }
  156.  
  157.  
  158. //
  159. // WCAP error handling helpers
  160. //
  161.  
  162. const g_wcapErrorCodes = [
  163.     /* -1 */ NS_OK, "Logout successful.",
  164.     /*  0 */ NS_OK, "Command successful.",
  165.     /*  1 */ calIWcapErrors.WCAP_LOGIN_FAILED, "Login failed. Invalid session ID.",
  166.     /*  2 */ calIWcapErrors.WCAP_LOGIN_OK_DEFAULT_CALENDAR_NOT_FOUND, "login.wcap was successful, but the default calendar for this user was not found. A new default calendar set to the userid was created.",
  167.     /*  3 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  168.     /*  4 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  169.     /*  5 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  170.     /*  6 */ calIWcapErrors.WCAP_DELETE_EVENTS_BY_ID_FAILED, "WCAP_DELETE_EVENTS_BY_ID_FAILED",
  171.     /*  7 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  172.     /*  8 */ calIWcapErrors.WCAP_SETCALPROPS_FAILED, "WCAP_SETCALPROPS_FAILED",
  173.     /*  9 */ calIWcapErrors.WCAP_FETCH_EVENTS_BY_ID_FAILED, "WCAP_FETCH_EVENTS_BY_ID_FAILED",
  174.     /* 10 */ calIWcapErrors.WCAP_CREATECALENDAR_FAILED, "WCAP_CREATECALENDAR_FAILED",
  175.     /* 11 */ calIWcapErrors.WCAP_DELETECALENDAR_FAILED, "WCAP_DELETECALENDAR_FAILED",
  176.     /* 12 */ calIWcapErrors.WCAP_ADDLINK_FAILED, "WCAP_ADDLINK_FAILED",
  177.     /* 13 */ calIWcapErrors.WCAP_FETCHBYDATERANGE_FAILED, "WCAP_FETCHBYDATERANGE_FAILED",
  178.     /* 14 */ calIWcapErrors.WCAP_STOREEVENTS_FAILED, "WCAP_STOREEVENTS_FAILED",
  179.     /* 15 */ calIWcapErrors.WCAP_STORETODOS_FAILED, "WCAP_STORETODOS_FAILED",
  180.     /* 16 */ calIWcapErrors.WCAP_DELETE_TODOS_BY_ID_FAILED, "WCAP_DELETE_TODOS_BY_ID_FAILED",
  181.     /* 17 */ calIWcapErrors.WCAP_FETCH_TODOS_BY_ID_FAILED, "WCAP_FETCH_TODOS_BY_ID_FAILED",
  182.     /* 18 */ calIWcapErrors.WCAP_FETCHCOMPONENTS_FAILED_BAD_TZID, "Command failed to find correct tzid. Applies to fetchcomponents_by_range.wcap, fetchevents_by_id.wcap, fetchtodos_by_id.wcap.",
  183.     /* 19 */ calIWcapErrors.WCAP_SEARCH_CALPROPS_FAILED, "WCAP_SEARCH_CALPROPS_FAILED",
  184.     /* 20 */ calIWcapErrors.WCAP_GET_CALPROPS_FAILED, "WCAP_GET_CALPROPS_FAILED",
  185.     /* 21 */ calIWcapErrors.WCAP_DELETECOMPONENTS_BY_RANGE_FAILED, "WCAP_DELETECOMPONENTS_BY_RANGE_FAILED",
  186.     /* 22 */ calIWcapErrors.WCAP_DELETEEVENTS_BY_RANGE_FAILED, "WCAP_DELETEEVENTS_BY_RANGE_FAILED",
  187.     /* 23 */ calIWcapErrors.WCAP_DELETETODOS_BY_RANGE_FAILED, "WCAP_DELETETODOS_BY_RANGE_FAILED",
  188.     /* 24 */ calIWcapErrors.WCAP_GET_ALL_TIMEZONES_FAILED, "WCAP_GET_ALL_TIMEZONES_FAILED",
  189.     /* 25 */ calIWcapErrors.WCAP_CREATECALENDAR_ALREADY_EXISTS_FAILED, "The command createcalendar.wcap failed. A calendar with that name already exists in the database.",
  190.     /* 26 */ calIWcapErrors.WCAP_SET_USERPREFS_FAILED, "WCAP_SET_USERPREFS_FAILED",
  191.     /* 27 */ calIWcapErrors.WCAP_CHANGE_PASSWORD_FAILED, "WCAP_CHANGE_PASSWORD_FAILED",
  192.     /* 28 */ calIWcapErrors.WCAP_ACCESS_DENIED_TO_CALENDAR, "Command failed. The user is denied access to a calendar.",
  193.     /* 29 */ calIWcapErrors.WCAP_CALENDAR_DOES_NOT_EXIST, "Command failed. The requested calendar does not exist in the database.",
  194.     /* 30 */ calIWcapErrors.WCAP_ILLEGAL_CALID_NAME, "createcalendar.wcap failed. Invalid calid passed in.",
  195.     /* 31 */ calIWcapErrors.WCAP_CANNOT_MODIFY_LINKED_EVENTS, "storeevents.wcap failed. The event to modify was a linked event.",
  196.     /* 32 */ calIWcapErrors.WCAP_CANNOT_MODIFY_LINKED_TODOS, "storetodos.wcap failed. The todo to modify was a linked todo.",
  197.     /* 33 */ calIWcapErrors.WCAP_CANNOT_SENT_EMAIL, "Command failed. Email notification failed. Usually caused by the server not being properly configured to send email. This can occur in storeevents.wcap, storetodos.wcap, deleteevents_by_id.wcap, deletetodos_by_id.wcap.",
  198.     /* 34 */ calIWcapErrors.WCAP_CALENDAR_DISABLED, "Command failed. The calendar is disabled in the database.",
  199.     /* 35 */ calIWcapErrors.WCAP_WRITE_IMPORT_FAILED, "Import failed when writing files to the server.",
  200.     /* 36 */ calIWcapErrors.WCAP_FETCH_BY_LAST_MODIFIED_FAILED, "WCAP_FETCH_BY_LAST_MODIFIED_FAILED",
  201.     /* 37 */ calIWcapErrors.WCAP_CAPI_NOT_SUPPORTED, "Failed trying to read from unsupported format calendar data.",
  202.     /* 38 */ calIWcapErrors.WCAP_CALID_NOT_SPECIFIED, "Calendar ID was not specified.",
  203.     /* 39 */ calIWcapErrors.WCAP_GET_FREEBUSY_FAILED, "WCAP_GET_FREEBUSY_FAILED",
  204.     /* 40 */ calIWcapErrors.WCAP_STORE_FAILED_DOUBLE_BOOKED, "If double booking is not allowed in this calendar, storeevents.wcap fails with this error when attempting to store an event in a time slot that was already filled.",
  205.     /* 41 */ calIWcapErrors.WCAP_FETCH_BY_ALARM_RANGE_FAILED, "WCAP_FETCH_BY_ALARM_RANGE_FAILED",
  206.     /* 42 */ calIWcapErrors.WCAP_FETCH_BY_ATTENDEE_ERROR_FAILED, "WCAP_FETCH_BY_ATTENDEE_ERROR_FAILED",
  207.     /* 43 */ calIWcapErrors.WCAP_ATTENDEE_GROUP_EXPANSION_CLIPPED, "An LDAP group being expanded was too large and exceeded the maximum number allowed in an expansion. The expansion stopped at the specified maximum limit. The maximum limit defaults to 200. To change the maximum limit, set the server configuration preference calstore.group.attendee.maxsize.",
  208.     /* 44 */ calIWcapErrors.WCAP_USERPREFS_ACCESS_DENIED, "Either the server does not allow this administrator access to get or modify user preferences, or the requester is not an administrator.",
  209.     /* 45 */ calIWcapErrors.WCAP_NOT_ALLOWED_TO_REQUEST_PUBLISH, "The requester was not an organizer of the event, and, therefore, is not allowed to edit the component using the PUBLISH or REQUEST method.",
  210.     /* 46 */ calIWcapErrors.WCAP_INSUFFICIENT_PARAMETERS, "The caller tried to invoke verifyevents_by_ids.wcap, or verifytodos_by_ids.wcap with insufficient arguments (mismatched number of uidÆs and ridÆs).",
  211.     /* 47 */ calIWcapErrors.WCAP_MUSTBEOWNER_OPERATION, "The user needs to be an owner or co-owner of the calendar in questions to complete this operation. (Probably related to private or confidential component.)",
  212.     /* 48 */ calIWcapErrors.WCAP_DWP_CONNECTION_FAILED, "GSE scheduling engine failed to make connection to DWP.",
  213.     /* 49 */ calIWcapErrors.WCAP_DWP_MAX_CONNECTION_REACHED, "Reached the maximum number of connections. When some of the connections are freed, users can successfully connect. Same as error 11001.",
  214.     /* 50 */ calIWcapErrors.WCAP_DWP_CANNOT_RESOLVE_CALENDAR, "Front end canÆt resolve to a particular back end. Same as error 11002.",
  215.     /* 51 */ calIWcapErrors.WCAP_DWP_BAD_DATA, "Generic response. Check all DWP servers. One might be down. Same as error 11003.",
  216.     /* 52 */ calIWcapErrors.WCAP_BAD_COMMAND, "The command sent in was not recognized. This is an internal only error code. It should not appear in the error logs.",
  217.     /* 53 */ calIWcapErrors.WCAP_NOT_FOUND, "Returned for all errors from a write to the Berkeley DB. This is an internal only error code. It should not appear in the error logs.",
  218.     /* 54 */ calIWcapErrors.WCAP_WRITE_IMPORT_CANT_EXPAND_CALID, "CanÆt expand calid when importing file.",
  219.     /* 55 */ calIWcapErrors.WCAP_GETTIME_FAILED, "Get server time failed.",
  220.     /* 56 */ calIWcapErrors.WCAP_FETCH_DELETEDCOMPONENTS_FAILED, "fetch_deletedcomponents.wcap failed.",
  221.     /* 57 */ calIWcapErrors.WCAP_FETCH_DELETEDCOMPONENTS_PARTIAL_RESULT, "Success but partial result.",
  222.     /* 58 */ calIWcapErrors.WCAP_WCAP_NO_SUCH_FORMAT, "Returned in any of the commands when supplied fmt-out is not a supported format.",
  223.     /* 59 */ calIWcapErrors.WCAP_COMPONENT_NOT_FOUND, "Returned when a fetch or delete is attempted that does not exist.",
  224.     /* 60 */ calIWcapErrors.WCAP_BAD_ARGUMENTS, "Currently used when attendee or organizer specified does not have a valid email address.",
  225.     /* 61 */ calIWcapErrors.WCAP_GET_USERPREFS_FAILED, "get_userprefs.wcap failed. The following error conditions returns error code 61: LDAP access denied, no results found, LDAP limit exceeded, LDAP connection failed.",
  226.     /* 62 */ calIWcapErrors.WCAP_WCAP_MODIFY_NO_EVENT, "storeevents.wcap issued with storetype set to 2 (WCAP_STORE_TYPE_MODIFY) and the event doesn\Æt exist.",
  227.     /* 63 */ calIWcapErrors.WCAP_WCAP_CREATE_EXISTS, "storeevents.wcap issued with storetype set to 1 (WCAP_STORE_TYPE_CREATE) and the event already exists.",
  228.     /* 64 */ calIWcapErrors.WCAP_WCAP_MODIFY_CANT_MAKE_COPY, "storevents.wcap issued and copy of event failed during processing.",
  229.     /* 65 */ calIWcapErrors.WCAP_STORE_FAILED_RECUR_SKIP, "One instance of a recurring event skips over another.",
  230.     /* 66 */ calIWcapErrors.WCAP_STORE_FAILED_RECUR_SAMEDAY, "Two instances of a recurring event canÆt occur on the same day.",
  231.     /* 67 */ calIWcapErrors.WCAP_BAD_ORG_ARGUMENTS, "Bad organizer arguments. orgCalid or orgEmail must be passed if any other \"org\" parameter is sent. That is, orgUID canÆt be sent alone on a storeevents.wcap or a storetodos.wcao command if it is trying about to \"create\" the event or task. Note, if no \"org\" information is passed, the organizer defaults to the calid being passed with the command.",
  232.     /* 68 */ calIWcapErrors.WCAP_STORE_FAILED_RECUR_PRIVACY, "Error returned if you try to change the privacy or transparency of a single instance in a recurring series.",
  233.     /* 69 */ calIWcapErrors.WCAP_LDAP_ERROR, "For get_calprops.wcap, when there is an error is getting LDAP derived token values (X-S1CS-CALPROPS-FB-INCLUDE, X-S1CS-CALPROPS-COMMON-NAME).",
  234.     /* 70 */ calIWcapErrors.WCAP_GET_INVITE_COUNT_FAILED, "Error in getting invite count (for get_calprops.wcap and fetchcomponents_by_range.wcap commands).",
  235.     /* 71 */ calIWcapErrors.WCAP_LIST_FAILED, "list.wcap failed.",
  236.     /* 72 */ calIWcapErrors.WCAP_LIST_SUBSCRIBED_FAILED, "list_subscribed.wcap failed.",
  237.     /* 73 */ calIWcapErrors.WCAP_SUBSCRIBE_FAILED, "subscribe.wcap failed.",
  238.     /* 74 */ calIWcapErrors.WCAP_UNSUBSCRIBE_FAILED, "unsubscribe.wcap failed.",
  239.     /* 75 */ calIWcapErrors.WCAP_ANONYMOUS_NOT_ALLOWED, "Command cannot be executed as anonymous. Used only for list.wcap, list_subscribed.wcap, subscribe.wcap, and unsubscribe.wcap commands.",
  240.     /* 76 */ calIWcapErrors.WCAP_ACCESS_DENIED, "Generated if a non-administrator user tries to read or set the calendar-owned list or the calendar-subscribed list of some other user, or if the option is not turned on in the server.",
  241.     /* 77 */ calIWcapErrors.WCAP_BAD_IMPORT_ARGUMENTS, "Incorrect parameter received by import.wcap.",
  242.     /* 78 */ calIWcapErrors.WCAP_READONLY_DATABASE, "Database is in read-only mode (returned for all attempts to write to the database).",
  243.     /* 79 */ calIWcapErrors.WCAP_ATTENDEE_NOT_ALLOWED_TO_REQUEST_ON_MODIFY, "Attendee is not allowed to modify an event with method=request.",
  244.     /* 80 */ calIWcapErrors.WCAP_TRANSP_RESOURCE_NOT_ALLOWED, "Resources do not permit the transparency parameter.",
  245.     /* 81 */ calIWcapErrors.WCAP_RECURRING_COMPONENT_NOT_FOUND, "Recurring component not found. Only happens when recurring=1 is passed in by fetch commands. This code is returned if part of the recurring series (either the master or an exception) is missing.",
  246.     /* new by WCAP 4.0: */
  247.     /* 82 */ calIWcapErrors.WCAP_BAD_MIME_TYPE,
  248.     "The mime headers supplied while storing the attachment using storeevents.wcap/storetodos.wcap is malformatted.",
  249.     /* 83 */ calIWcapErrors.WCAP_MISSING_BOUNDARY,
  250.     "While supplying attachments to the storeveents/storetodos commands the mime boundary was not found.",
  251.     /* 84 */ calIWcapErrors.WCAP_INVALID_ATTACHMENT,
  252.     "The attachment supplied to be stored on the server is malformatted.",
  253.     /* 85 */ calIWcapErrors.WCAP_ATTACH_DELETE_SUCCESS,
  254.     "All the attachments requested to be deleted from the server by supplying deleteattach were deleted successsfully.",
  255.     /* 86 */ calIWcapErrors.WCAP_ATTACH_DELETE_PARTIAL,
  256.     "Of All attachments requested to be deleted from the server by supplying deleteattach , only few were deleted successfully.",
  257.     /* 87 */ calIWcapErrors.WCAP_ATTACHMENT_NOT_FOUND,
  258.     "The attachent requested to be fetched or deleted from the server was not found.",
  259.     /* / new by WCAP 4.0 */
  260.     /* 88 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  261.     /* 89 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  262.     /* 90 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  263.     /* 91 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  264.     /* 92 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  265.     /* 93 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  266.     /* 94 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  267.     /* 95 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  268.     /* 96 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  269.     /* 97 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  270.     /* 98 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  271.     /* 99 */ NS_ERROR_INVALID_ARG, "No WCAP error code.",
  272.     /* 11000 */ calIWcapErrors.WCAP_CDWP_ERR_MAX_CONNECTION_REACHED, "Maximum connections to back-end database reached. As connections are freed up, users can connect to the back-end.",
  273.     /* 11001 */ calIWcapErrors.WCAP_CDWP_ERR_CANNOT_CONNECT, "Cannot connect to back-end server. Back-end machine might be down or DWP server is not up and running.",
  274.     /* 11002 */ calIWcapErrors.WCAP_CDWP_ERR_CANNOT_RESOLVE_CALENDAR, "Front-end canÆt resolve calendar to a particular back-end server.",
  275.     /* 11003 */ calIWcapErrors.WCAP_CDWP_ERR_BAD_DATA, "Bad data received from DWP connection. This is a generic formatting error. Check all DWP servers. One might be down.",
  276.     /* 11004 */ calIWcapErrors.WCAP_CDWP_ERR_DWPHOST_CTX_DOES_NOT_EXIST, "For the back-end host, context doesn\Æt exist in the context table.",
  277.     /* 11005 */ calIWcapErrors.WCAP_CDWP_ERR_HOSTNAME_NOT_RESOLVABLE, "DNS or NIS files, or hostname resolver is not set up properly or machine does not exist.",
  278.     /* 11006 */ calIWcapErrors.WCAP_CDWP_ERR_NO_DATA, "No data was received from reading the calendar properties from the DWP connection.",
  279.     /* 11007 */ calIWcapErrors.WCAP_CDWP_ERR_AUTH_FAILED, "DWP authentication failed.",
  280.     /* 11008 */ calIWcapErrors.WCAP_CDWP_ERR_CHECKVERSION_FAILED, "DWP version check failed."
  281.     ];
  282.  
  283. function wcapErrorToString(rc)
  284. {
  285.     if (isNaN(rc)) {
  286.         throw new Components.Exception("No known WCAP error code: " + rc.toString(0x10),
  287.                                        NS_ERROR_INVALID_ARG);
  288.     }
  289.     if (rc == calIWcapErrors.WCAP_NO_ERRNO)
  290.         return "No WCAP errno (missing X-NSCP-WCAP-ERRNO).";
  291.     
  292.     var index = (rc - calIWcapErrors.WCAP_ERROR_BASE + 1);
  293.     if (index >= 1 && index <= 108 &&
  294.         g_wcapErrorCodes[index * 2] != NS_ERROR_INVALID_ARG)
  295.     {
  296.         return g_wcapErrorCodes[(index * 2) + 1];
  297.     }
  298.     throw new Components.Exception("No known WCAP error code: " + rc.toString(0x10),
  299.                                    NS_ERROR_INVALID_ARG);
  300. }
  301.  
  302. function getWcapErrorCode(errno)
  303. {
  304.     if (errno == -5000) // semantically same error
  305.         errno = 59;
  306.     var index = -1;
  307.     if (errno >= -1 && errno <= 81)
  308.         index = (errno + 1);
  309.     else if (errno >= 11000 && errno <= 11008)
  310.         index = (errno - 11000 + 100 + 1);
  311.     if (index >= 0 &&
  312.         g_wcapErrorCodes[index * 2] != NS_ERROR_INVALID_ARG)
  313.     {
  314.         return g_wcapErrorCodes[index * 2];
  315.     }
  316.     throw new Components.Exception("No known WCAP error no: " + errno,
  317.                                    NS_ERROR_INVALID_ARG);
  318. }
  319.  
  320. function getWcapXmlErrno(xml)
  321. {
  322.     var elem = xml.getElementsByTagName("X-NSCP-WCAP-ERRNO");
  323.     if (elem) {
  324.         elem = elem.item(0);
  325.         if (elem)
  326.             return parseInt(elem.textContent);
  327.     }
  328.     // some commands just respond with an empty calendar, no errno. WTF.
  329.     // assume success:
  330.     return undefined;
  331. }
  332.  
  333. function getWcapIcalErrno(icalRootComp)
  334. {
  335.     var prop = icalRootComp.getFirstProperty("X-NSCP-WCAP-ERRNO");
  336.     if (prop)
  337.         return parseInt(prop.value);
  338.     // some commands just respond with an empty calendar, no errno. WTF.
  339.     // assume success:
  340.     return undefined;
  341. }
  342.  
  343. function checkWcapErrno(errno, expectedErrno)
  344. {
  345.     if (expectedErrno === undefined)
  346.         expectedErrno = 0; // i.e. Command successful.
  347.     if (errno !== undefined && errno != expectedErrno) {
  348.         var rc = getWcapErrorCode(errno);
  349.         throw new Components.Exception(wcapErrorToString(rc), rc);
  350.     }
  351. }
  352.  
  353. function checkWcapXmlErrno(xml, expectedErrno) {
  354.     checkWcapErrno(getWcapXmlErrno(xml), expectedErrno);
  355. }
  356.  
  357. function checkWcapIcalErrno(icalRootComp, expectedErrno) {
  358.     checkWcapErrno(getWcapIcalErrno(icalRootComp), expectedErrno);
  359. }
  360.  
  361. function errorToString(err)
  362. {
  363.     if (err) {
  364.         if (typeof(err) == "string")
  365.             return err;
  366.         if (err instanceof Error)
  367.             return err.message;
  368.         if (err instanceof nsIException)
  369.             return err.toString(); // xxx todo: or just message?
  370.         if (isNaN(err))
  371.             return ("[" + err + "] unknown error.");
  372.     }
  373.     // numeric codes:
  374.     switch (err) {
  375.     case undefined:
  376.     case null:
  377.     case NS_OK:
  378.         return "NS_OK";
  379.     case NS_ERROR_INVALID_ARG:
  380.         return "NS_ERROR_INVALID_ARG";
  381.     case Components.results.NS_ERROR_NO_INTERFACE:
  382.         return "NS_ERROR_NO_INTERFACE";
  383.     case Components.results.NS_ERROR_NOT_IMPLEMENTED:
  384.         return "NS_ERROR_NOT_IMPLEMENTED";
  385.     case Components.results.NS_ERROR_NOT_AVAILABLE:
  386.         return "NS_ERROR_NOT_AVAILABLE";
  387.     case Components.results.NS_ERROR_FAILURE:
  388.         return "NS_ERROR_FAILURE";
  389.     case Components.results.NS_ERROR_BASE:
  390.         return "NS_ERROR_BASE";
  391.     case Components.results.NS_ERROR_NOT_INITIALIZED:
  392.         return "NS_ERROR_NOT_INITIALIZED";
  393.     case Components.results.NS_ERROR_ALREADY_INITIALIZED:
  394.         return "NS_ERROR_ALREADY_INITIALIZED";
  395.     case Components.results.NS_ERROR_NULL_POINTER:
  396.         return "NS_ERROR_NULL_POINTER";
  397.     case Components.results.NS_ERROR_ABORT:
  398.         return "NS_ERROR_ABORT";
  399.     case Components.results.NS_ERROR_UNEXPECTED:
  400.         return "NS_ERROR_UNEXPECTED";
  401.     case Components.results.NS_ERROR_OUT_OF_MEMORY:
  402.         return "NS_ERROR_OUT_OF_MEMORY";
  403.     case Components.results.NS_ERROR_ILLEGAL_VALUE:
  404.         return "NS_ERROR_ILLEGAL_VALUE";
  405.     default:
  406.         // probe for WCAP error:
  407.         try {
  408.             return wcapErrorToString(err);
  409.         }
  410.         catch (exc) { // probe for netwerk error:
  411.             try {
  412.                 return netErrorToString(err);
  413.             }
  414.             catch (exc) {
  415.                 if (err & calIErrors.ERROR_BASE) {
  416.                     for (var err_ in calIErrors) {
  417.                         if (calIErrors[err_] == err)
  418.                             return err_;
  419.                     }
  420.                 }
  421.                 return ("[0x" + err.toString(0x10) + "] unknown error.");
  422.             }
  423.         }
  424.     }
  425. }
  426.  
  427.